/* 创建一个初始项目 */
const ora = require('ora')
const chalk = require('chalk')
const { resolve, exit, succeed } = require('../utils/index')
const downloadGit = require('download-git-repo')

module.exports = async function(name) {
  const url = resolve(name)

  /* 拉取模板 */
  await downTemplate(url)
  /* 成功 */
  createSucced(name)
}

/**
 * 下载目标
 * @param {String} destination 目录
 */
function downTemplate(destination) {
  const spinner = ora('下载模板中')

  spinner.start()
  return new Promise((resolve) => {
    downloadGit('direct: https://codeload.github.com/c121914yu/aha-micro-template/zip/refs/heads/master', destination, {}, function(err) {
      if (err) {
        exit('下载模板错误')
      }
      resolve(err)
    })
  })
    .finally(() => {
      spinner.stop()
      succeed('模板下载完成')
    })
}

/**
 * 安装依赖
 */
function createSucced(name) {
  console.log(`  cd ${ chalk.blueBright(name) } 到对应的目录下进行开发`)
  console.log(`  ${ chalk.blueBright('npm install') }   安装项目依赖`)
  console.log(`  ${ chalk.blueBright('npm run dev') }   运行开发环境`)
  console.log(`  ${ chalk.blueBright('npm run build') } 执行项目打包`)
}
